package com.hsy.linemoney.common;

import android.content.Context;
import android.content.SharedPreferences;

import java.util.Collections;
import java.util.Iterator;
import java.util.Map;

/**
 * 密码记录类
 * Created by MartinKent on 2016/11/28.
 */

public class PasswordSaver {
    private static boolean isEnabled = true;

    private static final String preferences_name = "preferences_name_farewrwdas";
    private static SharedPreferences preferences;

    public static void init(Context context) {
        preferences = context.getSharedPreferences(preferences_name, Context.MODE_APPEND);
    }

    public static void save(String user, String pass) {
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString(user, pass);
        editor.commit();
    }

    public static String[] getUserNameList() {
        Map<String, ?> data = preferences.getAll();
        Iterator<String> iterator = data.keySet().iterator();
        String[] users = new String[data.size()];
        int i = 0;
        while (iterator.hasNext()) {
            users[i] = iterator.next();
            i++;
        }
        return users;
    }

    public static String getPassword(String user) {
        return preferences.getString(user, null);
    }

    public static boolean isEnabled() {
        return isEnabled;
    }

    public static void setEnabled(boolean enabled) {
        PasswordSaver.isEnabled = enabled;
    }
}
